<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: How to run deep networks on Android device</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d2/d58/tutorial_table_of_content_dnn.html">Deep Neural Networks (dnn module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">How to run deep networks on Android device </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../de/d5d/tutorial_dnn_halide_scheduling.html">How to schedule your network for Halide backend</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../da/d9d/tutorial_dnn_yolo.html">YOLO DNNs</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Dmitry Kurtaev </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.3 </td></tr>
</table>
<h2>Introduction</h2>
<p>In this tutorial you'll know how to run deep learning networks on Android device using OpenCV deep learning module.</p>
<p>Tutorial was written for the following versions of corresponding software:</p><ul>
<li>Android Studio 2.3.3</li>
<li>OpenCV 3.3.0+</li>
</ul>
<h2>Requirements</h2>
<ul>
<li>Download and install Android Studio from <a href="https://developer.android.com/studio">https://developer.android.com/studio</a>.</li>
<li>Get the latest pre-built OpenCV for Android release from <a href="https://github.com/opencv/opencv/releases">https://github.com/opencv/opencv/releases</a> and unpack it (for example, <code>opencv-4.X.Y-android-sdk.zip</code>).</li>
<li>Download MobileNet object detection model from <a href="https://github.com/chuanqi305/MobileNet-SSD">https://github.com/chuanqi305/MobileNet-SSD</a>. We need a configuration file <code>MobileNetSSD_deploy.prototxt</code> and weights <code>MobileNetSSD_deploy.caffemodel</code>.</li>
</ul>
<h2>Create an empty Android Studio project</h2>
<ul>
<li>Open Android Studio. Start a new project. Let's call it <code>opencv_mobilenet</code>. <div class="image">
<img src="../../1_start_new_project.png" alt="1_start_new_project.png"/>
</div>
</li>
<li>Keep default target settings. <div class="image">
<img src="../../2_start_new_project.png" alt="2_start_new_project.png"/>
</div>
</li>
<li><p class="startli">Use "Empty Activity" template. Name activity as <code>MainActivity</code> with a corresponding layout <code>activity_main</code>. </p><div class="image">
<img src="../../3_start_new_project.png" alt="3_start_new_project.png"/>
</div>
<div class="image">
<img src="../../4_start_new_project.png" alt="4_start_new_project.png"/>
</div>
</li>
<li>Wait until a project was created. Go to <code>Run-&gt;Edit Configurations</code>. Choose <code>USB Device</code> as target device for runs. <div class="image">
<img src="../../5_setup.png" alt="5_setup.png"/>
</div>
 Plug in your device and run the project. It should be installed and launched successfully before we'll go next. <dl class="section note"><dt>Note</dt><dd>Read <a class="el" href="../../d9/d3f/tutorial_android_dev_intro.html">Introduction into Android Development</a> in case of problems.</dd></dl>
<div class="image">
<img src="../../6_run_empty_project.png" alt="6_run_empty_project.png"/>
</div>
</li>
</ul>
<h2>Add OpenCV dependency</h2>
<ul>
<li><p class="startli">Go to <code>File-&gt;New-&gt;Import module</code> and provide a path to <code>unpacked_OpenCV_package/sdk/java</code>. The name of module detects automatically. Disable all features that Android Studio will suggest you on the next window. </p><div class="image">
<img src="../../7_import_module.png" alt="7_import_module.png"/>
</div>
<div class="image">
<img src="../../8_import_module.png" alt="8_import_module.png"/>
</div>
</li>
<li><p class="startli">Open two files:</p><ol type="1">
<li><code>AndroidStudioProjects/opencv_mobilenet/app/build.gradle</code></li>
<li><code>AndroidStudioProjects/opencv_mobilenet/openCVLibrary330/build.gradle</code></li>
</ol>
<p class="startli">Copy both <code>compileSdkVersion</code> and <code>buildToolsVersion</code> from the first file to the second one.</p>
<p class="startli"><code>compileSdkVersion 14</code> -&gt; <code>compileSdkVersion 26</code></p>
<p class="startli"><code>buildToolsVersion "25.0.0"</code> -&gt; <code>buildToolsVersion "26.0.1"</code></p>
</li>
<li>Make the project. There is no errors should be at this point.</li>
<li><p class="startli">Go to <code>File-&gt;Project Structure</code>. Add OpenCV module dependency. </p><div class="image">
<img src="../../9_opencv_dependency.png" alt="9_opencv_dependency.png"/>
</div>
<div class="image">
<img src="../../10_opencv_dependency.png" alt="10_opencv_dependency.png"/>
</div>
</li>
<li>Install once an appropriate OpenCV manager from <code>unpacked_OpenCV_package/apk</code> to target device. <div class="fragment"><div class="line">adb install OpenCV_3.3.0_Manager_3.30_armeabi-v7a.apk</div></div><!-- fragment --></li>
<li>Congratulations! We're ready now to make a sample using OpenCV.</li>
</ul>
<h2>Make a sample</h2>
<p>Our sample will takes pictures from a camera, forwards it into a deep network and receives a set of rectangles, class identifiers and confidence values in <code>[0, 1]</code> range.</p>
<ul>
<li>First of all, we need to add a necessary widget which displays processed frames. Modify <code>app/src/main/res/layout/activity_main.xml</code>: <div class="fragment"><div class="line">&lt;?<span class="keyword">xml</span> <span class="keyword">version</span>=<span class="stringliteral">&quot;1.0&quot;</span> <span class="keyword">encoding</span>=<span class="stringliteral">&quot;utf-8&quot;</span>?&gt;</div><div class="line">&lt;<span class="keywordtype">FrameLayout</span> <span class="keyword">xmlns:android</span>=<span class="stringliteral">&quot;http://schemas.android.com/apk/res/android&quot;</span></div><div class="line">    <span class="keyword">xmlns:app</span>=<span class="stringliteral">&quot;http://schemas.android.com/apk/res-auto&quot;</span></div><div class="line">    <span class="keyword">xmlns:tools</span>=<span class="stringliteral">&quot;http://schemas.android.com/tools&quot;</span></div><div class="line">    <span class="keyword">android:layout_width</span>=<span class="stringliteral">&quot;match_parent&quot;</span></div><div class="line">    <span class="keyword">android:layout_height</span>=<span class="stringliteral">&quot;match_parent&quot;</span></div><div class="line">    <span class="keyword">tools:context</span>=<span class="stringliteral">&quot;org.opencv.samples.opencv_mobilenet.MainActivity&quot;</span>&gt;</div><div class="line"></div><div class="line">    &lt;<span class="keywordtype">org.opencv.android.JavaCameraView</span></div><div class="line">        <span class="keyword">android:id</span>=<span class="stringliteral">&quot;@+id/CameraView&quot;</span></div><div class="line">        <span class="keyword">android:layout_width</span>=<span class="stringliteral">&quot;match_parent&quot;</span></div><div class="line">        <span class="keyword">android:layout_height</span>=<span class="stringliteral">&quot;match_parent&quot;</span></div><div class="line">        <span class="keyword">android:visibility</span>=<span class="stringliteral">&quot;visible&quot;</span> /&gt;</div><div class="line">&lt;/<span class="keywordtype">FrameLayout</span>&gt;</div></div><!-- fragment --></li>
<li>Put downloaded <code>MobileNetSSD_deploy.prototxt</code> and <code>MobileNetSSD_deploy.caffemodel</code> into <code>app/build/intermediates/assets/debug</code> folder.</li>
<li>Modify <code>/app/src/main/AndroidManifest.xml</code> to enable full-screen mode, set up a correct screen orientation and allow to use a camera. <div class="fragment"><div class="line">&lt;?<span class="keyword">xml</span> <span class="keyword">version</span>=<span class="stringliteral">&quot;1.0&quot;</span> <span class="keyword">encoding</span>=<span class="stringliteral">&quot;utf-8&quot;</span>?&gt;</div><div class="line">&lt;<span class="keywordtype">manifest</span> <span class="keyword">xmlns:android</span>=<span class="stringliteral">&quot;http://schemas.android.com/apk/res/android&quot;</span></div><div class="line">          <span class="keyword">package</span>=<span class="stringliteral">&quot;org.opencv.samples.opencv_mobilenet&quot;</span></div><div class="line">&gt;</div><div class="line"></div><div class="line">    &lt;<span class="keywordtype">application</span></div><div class="line">        <span class="keyword">android:allowBackup</span>=<span class="stringliteral">&quot;true&quot;</span></div><div class="line">        <span class="keyword">android:icon</span>=<span class="stringliteral">&quot;@mipmap/ic_launcher&quot;</span></div><div class="line">        <span class="keyword">android:label</span>=<span class="stringliteral">&quot;@string/app_name&quot;</span></div><div class="line">        <span class="keyword">android:roundIcon</span>=<span class="stringliteral">&quot;@mipmap/ic_launcher_round&quot;</span></div><div class="line">        <span class="keyword">android:supportsRtl</span>=<span class="stringliteral">&quot;true&quot;</span></div><div class="line">        <span class="keyword">android:theme</span>=<span class="stringliteral">&quot;@style/Theme.AppCompat.NoActionBar&quot;</span>&gt;  <span class="comment">&lt;!--Full screen mode--&gt;</span></div><div class="line">        &lt;<span class="keywordtype">activity</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;.MainActivity&quot;</span></div><div class="line">            <span class="keyword">android:screenOrientation</span>=<span class="stringliteral">&quot;landscape&quot;</span>&gt;  <span class="comment">&lt;!--Screen orientation--&gt;</span></div><div class="line">            &lt;<span class="keywordtype">intent-filter</span>&gt;</div><div class="line">                &lt;<span class="keywordtype">action</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.intent.action.MAIN&quot;</span> /&gt;</div><div class="line">                &lt;<span class="keywordtype">category</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.intent.category.LAUNCHER&quot;</span> /&gt;</div><div class="line">            &lt;/<span class="keywordtype">intent-filter</span>&gt;</div><div class="line">        &lt;/<span class="keywordtype">activity</span>&gt;</div><div class="line">    &lt;/<span class="keywordtype">application</span>&gt;</div><div class="line"></div><div class="line">    <span class="comment">&lt;!--Allow to use a camera--&gt;</span></div><div class="line">    &lt;<span class="keywordtype">uses-permission</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.permission.CAMERA&quot;</span>/&gt;</div><div class="line">    &lt;<span class="keywordtype">uses-feature</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.hardware.camera&quot;</span> <span class="keyword">android:required</span>=<span class="stringliteral">&quot;false&quot;</span>/&gt;</div><div class="line">    &lt;<span class="keywordtype">uses-feature</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.hardware.camera.autofocus&quot;</span> <span class="keyword">android:required</span>=<span class="stringliteral">&quot;false&quot;</span>/&gt;</div><div class="line">    &lt;<span class="keywordtype">uses-feature</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.hardware.camera.front&quot;</span> <span class="keyword">android:required</span>=<span class="stringliteral">&quot;false&quot;</span>/&gt;</div><div class="line">    &lt;<span class="keywordtype">uses-feature</span> <span class="keyword">android:name</span>=<span class="stringliteral">&quot;android.hardware.camera.front.autofocus&quot;</span> <span class="keyword">android:required</span>=<span class="stringliteral">&quot;false&quot;</span>/&gt;</div><div class="line"></div><div class="line">&lt;/<span class="keywordtype">manifest</span>&gt;</div></div><!-- fragment --></li>
<li>Replace content of <code>app/src/main/java/org/opencv/samples/opencv_mobilenet/MainActivity.java</code>: <div class="fragment"><div class="line"><span class="keyword">package </span>org.opencv.samples.opencv_mobilenet;</div><div class="line"></div><div class="line"><span class="keyword">import</span> android.content.Context;</div><div class="line"><span class="keyword">import</span> android.content.res.AssetManager;</div><div class="line"><span class="keyword">import</span> android.os.Bundle;</div><div class="line"><span class="keyword">import</span> android.support.v7.app.AppCompatActivity;</div><div class="line"><span class="keyword">import</span> android.util.Log;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.opencv.android.BaseLoaderCallback;</div><div class="line"><span class="keyword">import</span> org.opencv.android.CameraBridgeViewBase;</div><div class="line"><span class="keyword">import</span> org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;</div><div class="line"><span class="keyword">import</span> org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;</div><div class="line"><span class="keyword">import</span> org.opencv.android.LoaderCallbackInterface;</div><div class="line"><span class="keyword">import</span> org.opencv.android.OpenCVLoader;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Point;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Scalar;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Size;</div><div class="line"><span class="keyword">import</span> org.opencv.dnn.Net;</div><div class="line"><span class="keyword">import</span> org.opencv.dnn.Dnn;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.io.BufferedInputStream;</div><div class="line"><span class="keyword">import</span> java.io.File;</div><div class="line"><span class="keyword">import</span> java.io.FileOutputStream;</div><div class="line"><span class="keyword">import</span> java.io.IOException;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>MainActivity <span class="keyword">extends</span> AppCompatActivity implements CvCameraViewListener2 {</div><div class="line"></div><div class="line">    <span class="comment">// Initialize OpenCV manager.</span></div><div class="line">    <span class="keyword">private</span> BaseLoaderCallback mLoaderCallback = <span class="keyword">new</span> BaseLoaderCallback(<span class="keyword">this</span>) {</div><div class="line">        @Override</div><div class="line">        <span class="keyword">public</span> <span class="keywordtype">void</span> onManagerConnected(<span class="keywordtype">int</span> status) {</div><div class="line">            <span class="keywordflow">switch</span> (status) {</div><div class="line">                <span class="keywordflow">case</span> LoaderCallbackInterface.SUCCESS: {</div><div class="line">                    Log.i(TAG, <span class="stringliteral">&quot;OpenCV loaded successfully&quot;</span>);</div><div class="line">                    mOpenCvCameraView.enableView();</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                }</div><div class="line">                <span class="keywordflow">default</span>: {</div><div class="line">                    super.onManagerConnected(status);</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">    };</div><div class="line"></div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> onResume() {</div><div class="line">        super.onResume();</div><div class="line">        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, <span class="keyword">this</span>, mLoaderCallback);</div><div class="line">    }</div><div class="line"></div><div class="line">    @Override</div><div class="line">    <span class="keyword">protected</span> <span class="keywordtype">void</span> onCreate(Bundle savedInstanceState) {</div><div class="line">        super.onCreate(savedInstanceState);</div><div class="line">        setContentView(R.layout.activity_main);</div><div class="line"></div><div class="line">        <span class="comment">// Set up camera listener.</span></div><div class="line">        mOpenCvCameraView = (CameraBridgeViewBase)findViewById(R.id.CameraView);</div><div class="line">        mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);</div><div class="line">        mOpenCvCameraView.setCvCameraViewListener(<span class="keyword">this</span>);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Load a network.</span></div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> onCameraViewStarted(<span class="keywordtype">int</span> width, <span class="keywordtype">int</span> height) {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> proto = getPath(<span class="stringliteral">&quot;MobileNetSSD_deploy.prototxt&quot;</span>, <span class="keyword">this</span>);</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> weights = getPath(<span class="stringliteral">&quot;MobileNetSSD_deploy.caffemodel&quot;</span>, <span class="keyword">this</span>);</div><div class="line">        net = Dnn.readNetFromCaffe(proto, weights);</div><div class="line">        Log.i(TAG, <span class="stringliteral">&quot;Network loaded successfully&quot;</span>);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">public</span> Mat onCameraFrame(CvCameraViewFrame inputFrame) {</div><div class="line">        <span class="keyword">final</span> <span class="keywordtype">int</span> IN_WIDTH = 300;</div><div class="line">        <span class="keyword">final</span> <span class="keywordtype">int</span> IN_HEIGHT = 300;</div><div class="line">        <span class="keyword">final</span> <span class="keywordtype">float</span> WH_RATIO = (float)IN_WIDTH / IN_HEIGHT;</div><div class="line">        <span class="keyword">final</span> <span class="keywordtype">double</span> IN_SCALE_FACTOR = 0.007843;</div><div class="line">        <span class="keyword">final</span> <span class="keywordtype">double</span> MEAN_VAL = 127.5;</div><div class="line">        <span class="keyword">final</span> <span class="keywordtype">double</span> THRESHOLD = 0.2;</div><div class="line"></div><div class="line">        <span class="comment">// Get a new frame</span></div><div class="line">        Mat frame = inputFrame.rgba();</div><div class="line">        Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);</div><div class="line"></div><div class="line">        <span class="comment">// Forward image through network.</span></div><div class="line">        Mat blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR,</div><div class="line">                <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(IN_WIDTH, IN_HEIGHT),</div><div class="line">                <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(MEAN_VAL, MEAN_VAL, MEAN_VAL), <span class="comment">/*swapRB*/</span><span class="keyword">false</span>, <span class="comment">/*crop*/</span><span class="keyword">false</span>);</div><div class="line">        net.setInput(blob);</div><div class="line">        Mat detections = net.forward();</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> cols = frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>();</div><div class="line">        <span class="keywordtype">int</span> rows = frame.rows();</div><div class="line"></div><div class="line">        detections = detections.reshape(1, (<span class="keywordtype">int</span>)detections.total() / 7);</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; detections.rows(); ++i) {</div><div class="line">            <span class="keywordtype">double</span> confidence = detections.get(i, 2)[0];</div><div class="line">            <span class="keywordflow">if</span> (confidence &gt; THRESHOLD) {</div><div class="line">                <span class="keywordtype">int</span> classId = (int)detections.get(i, 1)[0];</div><div class="line"></div><div class="line">                <span class="keywordtype">int</span> left   = (int)(detections.get(i, 3)[0] * cols);</div><div class="line">                <span class="keywordtype">int</span> top    = (int)(detections.get(i, 4)[0] * rows);</div><div class="line">                <span class="keywordtype">int</span> right  = (int)(detections.get(i, 5)[0] * cols);</div><div class="line">                <span class="keywordtype">int</span> bottom = (int)(detections.get(i, 6)[0] * rows);</div><div class="line"></div><div class="line">                <span class="comment">// Draw rectangle around detected object.</span></div><div class="line">                Imgproc.rectangle(frame, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(left, top), <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(right, bottom),</div><div class="line">                                  <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 255, 0));</div><div class="line">                <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> label = classNames[classId] + <span class="stringliteral">&quot;: &quot;</span> + confidence;</div><div class="line">                <span class="keywordtype">int</span>[] baseLine = <span class="keyword">new</span> <span class="keywordtype">int</span>[1];</div><div class="line">                <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a> labelSize = Imgproc.getTextSize(label, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);</div><div class="line"></div><div class="line">                <span class="comment">// Draw background for label.</span></div><div class="line">                Imgproc.rectangle(frame, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(left, top - labelSize.height),</div><div class="line">                                  <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(left + labelSize.width, top + baseLine[0]),</div><div class="line">                                  <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255, 255, 255), Imgproc.FILLED);</div><div class="line">                <span class="comment">// Write class name and confidence.</span></div><div class="line">                Imgproc.putText(frame, label, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(left, top),</div><div class="line">                        Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 0, 0));</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keywordflow">return</span> frame;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> onCameraViewStopped() {}</div><div class="line"></div><div class="line">    <span class="comment">// Upload file to storage and return a path.</span></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> getPath(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> file, Context context) {</div><div class="line">        AssetManager assetManager = context.getAssets();</div><div class="line"></div><div class="line">        BufferedInputStream inputStream = null;</div><div class="line">        <span class="keywordflow">try</span> {</div><div class="line">            <span class="comment">// Read data from assets.</span></div><div class="line">            inputStream = <span class="keyword">new</span> BufferedInputStream(assetManager.open(file));</div><div class="line">            byte[] data = <span class="keyword">new</span> byte[inputStream.available()];</div><div class="line">            inputStream.read(data);</div><div class="line">            inputStream.close();</div><div class="line"></div><div class="line">            <span class="comment">// Create copy file in storage.</span></div><div class="line">            File outFile = <span class="keyword">new</span> File(context.getFilesDir(), file);</div><div class="line">            FileOutputStream os = <span class="keyword">new</span> FileOutputStream(outFile);</div><div class="line">            os.write(data);</div><div class="line">            os.close();</div><div class="line">            <span class="comment">// Return a path to file which may be read in common way.</span></div><div class="line">            <span class="keywordflow">return</span> outFile.getAbsolutePath();</div><div class="line">        } <span class="keywordflow">catch</span> (IOException ex) {</div><div class="line">            Log.i(TAG, <span class="stringliteral">&quot;Failed to upload a file&quot;</span>);</div><div class="line">        }</div><div class="line">        <span class="keywordflow">return</span> <span class="stringliteral">&quot;&quot;</span>;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> TAG = <span class="stringliteral">&quot;OpenCV/Sample/MobileNet&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] classNames = {<span class="stringliteral">&quot;background&quot;</span>,</div><div class="line">            <span class="stringliteral">&quot;aeroplane&quot;</span>, <span class="stringliteral">&quot;bicycle&quot;</span>, <span class="stringliteral">&quot;bird&quot;</span>, <span class="stringliteral">&quot;boat&quot;</span>,</div><div class="line">            <span class="stringliteral">&quot;bottle&quot;</span>, <span class="stringliteral">&quot;bus&quot;</span>, <span class="stringliteral">&quot;car&quot;</span>, <span class="stringliteral">&quot;cat&quot;</span>, <span class="stringliteral">&quot;chair&quot;</span>,</div><div class="line">            <span class="stringliteral">&quot;cow&quot;</span>, <span class="stringliteral">&quot;diningtable&quot;</span>, <span class="stringliteral">&quot;dog&quot;</span>, <span class="stringliteral">&quot;horse&quot;</span>,</div><div class="line">            <span class="stringliteral">&quot;motorbike&quot;</span>, <span class="stringliteral">&quot;person&quot;</span>, <span class="stringliteral">&quot;pottedplant&quot;</span>,</div><div class="line">            <span class="stringliteral">&quot;sheep&quot;</span>, <span class="stringliteral">&quot;sofa&quot;</span>, <span class="stringliteral">&quot;train&quot;</span>, <span class="stringliteral">&quot;tvmonitor&quot;</span>};</div><div class="line"></div><div class="line">    <span class="keyword">private</span> Net net;</div><div class="line">    <span class="keyword">private</span> CameraBridgeViewBase mOpenCvCameraView;</div><div class="line">}</div></div><!-- fragment --></li>
<li>Launch an application and make a fun! <div class="image">
<img src="../../11_demo.jpg" alt="11_demo.jpg"/>
</div>
 </li>
</ul>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:34 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
